//
//  TabBarItemIVew.swift
//  ScrollTabBarController
//
//  Created by 好米有易 on 2017/11/13.
//  Copyright © 2017年 好米有易. All rights reserved.
//

import UIKit

protocol TabBarItemViewDelegate {
    
    func tapWith(index: NSInteger)
}

let buttonH: CGFloat = 49

class TabBarItemIVew: UIView {
    
    open var delegate: TabBarItemViewDelegate?
    
    /// 按钮
   fileprivate lazy var buttons: [UIButton] = {
        
        let btn = NSMutableArray()
        
        return btn as! [UIButton]
    }()
    
    
    /// 控制器
   fileprivate lazy var items: [UIViewController] = {
        
        let items = NSMutableArray()
        
        return items as! [UIViewController]
    }()
    
   /// 当前的Item
   fileprivate var currentItem: UIButton?
    
   open var itemTintColor: UIColor = UIColor.red
    
    
    /// headerLine
   fileprivate lazy var headerLine: UIView = {
        
        let line = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDHT, height: 1 / SACAL))
        
        line.backgroundColor = UIColor.darkGray
        
        return line
    }()
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        setupUI()
        
        backgroundColor = UIColor.groupTableViewBackground
        
        
        
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension TabBarItemIVew {
    
    /// 重置当前选中按钮 刷新目标按钮
    ///
    /// - Parameter index: 目标按钮下标
    func resetCurrentItem(index: NSInteger){
        
        
        //重置当前的按钮
        freshCurrentItem(index: (self.currentItem?.tag)!)
        
        let targetBtn: UIButton = buttons[index]

        freshTargetItem(sender: targetBtn)
        
    }
    
}

// MARK: - action
extension TabBarItemIVew {
    
    @objc func buttonAction(sender: UIButton) {
        
        
        if sender.isSelected == true {
            return
        }
        
       
        
        freshCurrentItem(index: (self.currentItem?.tag)!)
        
        if (self.delegate != nil) {
            self.delegate?.tapWith(index: sender.tag)
        }
        
        freshTargetItem(sender: sender)
        
    }
    
    
    /// 刷新当前控制器
    fileprivate func freshCurrentItem(index: NSInteger)   {
        /// 获取当前控制器
        let curentVC: UIViewController = items[index]
        
        self.currentItem?.setImage(curentVC.tabBarItem.image, for: .normal)
        
        self.currentItem?.isSelected = false
        
        self.currentItem?.setTitleColor(UIColor.darkGray, for: .normal)
    }
    
    /// 刷新目标控制
    ///
    /// - Parameter sender: Item
    fileprivate func freshTargetItem(sender: UIButton) {
        /// 设置目标控制器
        self.currentItem = sender
        
        sender.isSelected = true
        
        /// 获取目标控制器
        let targetVC: UIViewController = items[sender.tag]
        
        sender.setImage(targetVC.tabBarItem.selectedImage, for: .normal)
        
        sender.setTitleColor(itemTintColor, for: .normal)
        
    }
    
}



// MARK: - UI
extension TabBarItemIVew {
    
    
    fileprivate func setupUI() {
        
        addSubview(headerLine)
        
    }
    
    /// 加载ItemUI
    ///
    /// - Parameter items: Item对应的控制器
    func load(items: [UIViewController]) {
        
        self.items = NSMutableArray(array: items) as! [UIViewController]
        
        if items.count == 0 {
            return
        }
        
        if buttons.count > 0 {
            
            buttons.removeAll()
            
        }
        
        /// 计算Item的宽度
        let buttonW: CGFloat = SCREEN_WIDHT / CGFloat(items.count)
        
        for i in 0...items.count - 1 {
            
            let vc: UIViewController = items[i]
            
            let button = UIButton(type: .custom)
            
            button.frame = CGRect(x: buttonW * CGFloat(i), y: 0.0, width: buttonW, height: buttonH)
            
            guard let image = i == 0 ? vc.tabBarItem.selectedImage : vc.tabBarItem.image else {return}
            
            button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
            
            button.titleLabel?.font = UIFont.systemFont(ofSize: 12);
            
            guard let title = vc.title else {return}
            
            button.set(image: image, title: title, titlePosition: .bottom, additionalSpacing: 5, state: .normal)
         
            let titleColor = i == 0 ? itemTintColor : UIColor.lightGray
            
            button.setTitleColor(titleColor, for: .normal)
            
            button.tag = i;
            
            button.backgroundColor = UIColor.white
            
            buttons.append(button)
            
            addSubview(button)
            
            sendSubview(toBack: button)
        }
        
        
        // MARK: - 设置默认选中的Item
        self.currentItem = self.buttons[0];
        
    }
    
    
    
}




